//
//  ViewController.m
//  Test1
//
//  Created by 王冲 on 2019/1/30.
//  Copyright © 2019年 JK科技有限公司. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 100)];
    label.backgroundColor = [UIColor brownColor];
    label.text = @"我是Test1";
    label.font = [UIFont systemFontOfSize:25.f];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMidX(label.frame)-50, CGRectGetMaxY(label.frame)+50, 100, 50)];
    [button setBackgroundColor:[UIColor brownColor]];
    [button setTitle:@"跳转Test2" forState:UIControlStateNormal];
    button.tag = 101;
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(button.frame)+50, [UIScreen mainScreen].bounds.size.width-40, 50)];
    button2.tag = 102;
    [button2 setBackgroundColor:[UIColor brownColor]];
    [button2 setTitle:@"跳转Test3的ViewController2" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(button2.frame)+50, [UIScreen mainScreen].bounds.size.width-40, 50)];
    button3.tag = 103;
    [button3 setBackgroundColor:[UIColor brownColor]];
    [button3 setTitle:@"跳转Test3的ViewController3" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];

}




-(void)click:(UIButton *)sender{
    
    NSLog(@"跳转到Test");
    
    switch (sender.tag) {
        case 101:
            // 跳转Test2
            [self jumpUrl:@"test2://"];
            break;
        case 102:
            // 跳转Test3的ViewController2
            [self jumpUrl:@"test3://test1/vc2"];
            break;
        case 103:
            // 跳转Test3的ViewController3
            [self jumpUrl:@"test3://test1/vc3"];
            break;
        default:
            break;
    }
    
}

-(void)jumpUrl:(NSString *)urlStr{
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    // 注意: 跳转之前, 可以使用 canOpenURL: 判断是否可以跳转
    if (![[UIApplication sharedApplication]canOpenURL:url]) {
        return;
    }
    
    if (@available(iOS 10.0, *)){
        
        [[UIApplication sharedApplication]openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey:@YES} completionHandler:^(BOOL success) {
            
            if (success) {
                NSLog(@"10以后可以跳转url");
            }else{
                NSLog(@"10以后不可以跳转url");
            }
        }];
    }else{
        
        BOOL success = [[UIApplication sharedApplication]openURL:url];
        
        if (success) {
            NSLog(@"10以前可以跳转url");
        }else{
            NSLog(@"10以前不可以跳转url");
        }
    }
}


@end
